//
//  DownloadAlertView.swift
//  ptbuy
//
//  Created by MyMac on 2024/11/27.
//

import SwiftUI

// 异步下载弹框视图
struct DownloadAlertView: View {
    @Binding var isWinShowingAlert: Bool
    @Binding var showAlert: Bool
    @State private var isDownloading = false
    @State private var progress: Double = 0.0
    
    // 异步任务控制
    func startDownload() {
        Task {
            isDownloading = true
            for i in 1...10 {
                // 模拟下载进度更新
                try? await Task.sleep(nanoseconds: 500_000_000)
                let currentProgress = Double(i)
                progress = currentProgress
                if i == 10 {
                    isDownloading = false
                    isWinShowingAlert = false
                    showAlert = true
                }
            }
           
        }
    }
    
    var body: some View {
        VStack {
            ProgressView("Downloading...", value: progress, total: 10)
                .progressViewStyle(LinearProgressViewStyle())
                .padding()
            Text("\(Int(progress))%")
                .font(.headline)
                .padding(.top, 10)
        }.onAppear{
            startDownload()
        }
        .frame(width: UIScreen.main.bounds.width-40, height: 150)
        .background(Color.white)
        .cornerRadius(10)
        .shadow(radius: 10)
    }
}
